﻿using DeviceManager.ContentPage;
using DeviceManager.ContentPage.SysMng;
using DeviceManager.ContentPage.UserMng;
using DeviceManager.DB;
using DeviceManager.Logic;
using DeviceManager.Login;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace DeviceManager
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        private System.Timers.Timer tmr = null;
        public MainWindow()
        {
             InitializeComponent();
            grid_MainContent.Visibility = Visibility.Hidden;
            this.Loaded += MainWindow_Loaded;
            this.Closed += MainWindow_Closed;
        }

        private void MainWindow_Closed(object sender, EventArgs e)
        {
            ManagerDBCtrl.GetMng().Dispose();
        }

        private void MainWindow_Loaded(object sender, RoutedEventArgs e)
        {
            try
            {
                if (DB.ManagerDBCtrl.GetMng() == null)
                {
                    App.Current.Shutdown();
                }
            }
            catch(Exception ex)
            {
                MessageBox.Show(ex.Message);
            }

            Window_UserLogin login = new Window_UserLogin();
            login.Owner = this;
            login.WindowStartupLocation = WindowStartupLocation.CenterOwner;
            login.ShowDialog();
            
            
            if (LoginManager.GetMng().LoginUserInfo!=null)
            {
                //初始化界面
                grid_MainContent.Visibility = Visibility.Visible;
                btnMaximize_Click(null, null);
                blt_HomePage_Click(blt_HomePage, null);

                tb_userinfo.Text = string.Format("欢迎您:{0}({1})", LoginManager.GetMng().LoginUserInfo.nickName, LoginManager.GetMng().LoginUserInfo.loginName);

                tmr = new System.Timers.Timer();
                tmr.Interval = 1000;
                tmr.Elapsed += Tmr_Elapsed;
                tmr.Start();
            }
            else
            {
                App.Current.Shutdown();
            }
        }

        private void Tmr_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
        {
            this.Dispatcher.Invoke(new ThreadStart(delegate ()
            {
                tb_time.Text = DateTime.Now.ToString("yyyy年MM月dd日 hh:mm:ss");
            }), null);
        }

        internal void InitUI()
        {
            
        }


        #region 窗体基础操作

        Rect rcnormal;//定义一个全局rect记录还原状态下窗口的位置和大小。
        bool isDrop = true;
        /// <summary>
        /// 最大化
        /// </summary>
        private void btnMaximize_Click(object sender, RoutedEventArgs e)
        {
            isDrop = false;
            this.btnMaximize.Visibility = Visibility.Collapsed;
            this.btnNormal.Visibility = Visibility.Visible;
            rcnormal = new Rect(this.Left, this.Top, this.Width, this.Height);//保存下当前位置与大小
            this.Left = 0;//设置位置
            this.Top = 0;
            Rect rc = SystemParameters.WorkArea;//获取工作区大小
            this.Width = rc.Width;
            this.Height = rc.Height;
        }
        /// <summary>
        /// 还原
        /// </summary>
        private void btnNormal_Click(object sender, RoutedEventArgs e)
        {
            isDrop = true;
            this.Left = rcnormal.Left;
            this.Top = rcnormal.Top;
            this.Width = rcnormal.Width;
            this.Height = rcnormal.Height;
            this.btnMaximize.Visibility = Visibility.Visible;
            this.btnNormal.Visibility = Visibility.Collapsed;
        }

        private void btnClose_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
            App.Current.Shutdown();
        }

        private void Grid_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            try
            {
                if (isDrop)
                {
                    this.DragMove();
                }
            }
            catch { }
        }
        #endregion

        private void blt_first_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            EditDepInfo dep = new EditDepInfo();
            if (dep.ShowDialog() == true)
            {
             
            }
        }

        private void blt_systemMng_Click(object sender, RoutedEventArgs e)
        {
            Button blt = sender as Button;
            if (blt.Tag == null)
            {
                blt.Tag = new PageSystemSetting();
                tb_view.Navigate(blt.Tag);
            }
            else
            {
                tb_view.Navigate(blt.Tag);
            }
        }

        private void blt_ForecastMng_Click(object sender, RoutedEventArgs e)
        {

        }

        private void blt_StatisticsMng_Click(object sender, RoutedEventArgs e)
        {
            Button blt = sender as Button;
            if (blt.Tag == null)
            {
                blt.Tag = new StatisticsPage();
                tb_view.Navigate(blt.Tag);
            }
            else
            {
                tb_view.Navigate(blt.Tag);
            }
        }

        private void blt_performanceMng_Click(object sender, RoutedEventArgs e)
        {
            Button blt = sender as Button;
            if (blt.Tag == null)
            {
                blt.Tag = new PerformancePage();
                tb_view.Navigate(blt.Tag);
            }
            else
            {
                tb_view.Navigate(blt.Tag);
            }
        }

        private void blt_deviceMng_Click(object sender, RoutedEventArgs e)
        {
            Button blt = sender as Button;
            if (blt.Tag == null)
            {
                blt.Tag = new DeviceManagerPage();
                tb_view.Navigate(blt.Tag);
            }
            else
            {
                tb_view.Navigate(blt.Tag);
            }
        }

        private void blt_HomePage_Click(object sender, RoutedEventArgs e)
        {
            Button blt = sender as Button;
            if (blt.Tag == null)
            {
                blt.Tag = new HomePage();
                tb_view.Navigate(blt.Tag);
            }
            else
            {
                tb_view.Navigate(blt.Tag);
            }
        }
    }
}
